package com.EAsystem.service.Impl;

import com.EAsystem.entity.Teacher;
import com.EAsystem.mapper.TeacherMapper;
import com.EAsystem.service.TeacherService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 教师服务实现类
 */
@Service
public class TeacherServiceImpl implements TeacherService {

    @Autowired
    private TeacherMapper teacherMapper;
    
    private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    @Override
    public PageInfo<Teacher> findAllByPage(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Teacher> teachers = teacherMapper.findAllTeachersWithPage();
        return new PageInfo<>(teachers);
    }

    @Override
    public Teacher findById(Integer id) {
        return teacherMapper.findTeacherById(id);
    }

    @Override
    public Teacher findByTeacherNo(String teacherNo) {
        return teacherMapper.findTeacherByTeacherNo(teacherNo);
    }

    @Override
    public Teacher findByUserId(Integer userId) {
        return teacherMapper.findTeacherByUserId(userId);
    }



    @Override
    public PageInfo<Teacher> findByCondition(Teacher teacher, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Teacher> teachers = teacherMapper.findTeachersByCondition(teacher);
        return new PageInfo<>(teachers);
    }

    @Override
    @Transactional
    public boolean add(Teacher teacher) {
        // 设置创建时间和更新时间
        String currentTime = sdf.format(new Date());
        teacher.setCreateTime(currentTime);
        teacher.setUpdateTime(currentTime);
        
        return teacherMapper.insertTeacher(teacher) > 0;
    }

    @Override
    @Transactional
    public boolean update(Teacher teacher) {
        // 设置更新时间
        teacher.setUpdateTime(sdf.format(new Date()));
        
        return teacherMapper.updateTeacher(teacher) > 0;
    }

    @Override
    @Transactional
    public boolean delete(Integer id) {
        return teacherMapper.deleteTeacherById(id) > 0;
    }


    
    
}